﻿using System.Web;
using System.Web.Mvc;
using System.Configuration;

namespace mBlog.Infrastructure.Filters
{
    public class UserInfoParseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
            
            if (request.Cookies[ConfigurationManager.AppSettings["UserInfoCookieKey"]] != null)
            {
                var httpCookie = request.Cookies[ConfigurationManager.AppSettings["UserInfoCookieKey"]];
                if (httpCookie != null)
                {
                    string userInfo = httpCookie.Value;
                    string[] infos = userInfo.Split('&');

                    filterContext.RouteData.Values.Add("User", infos[1] + infos[0]);
                    filterContext.RouteData.Values.Add("Email", infos[2]);
                    if (infos.Length > 3)
                    {
                        filterContext.RouteData.Values.Add("ImageSrc", infos[3]);
                    }
                }
            }

            base.OnActionExecuting(filterContext);
        }
    }
}